function CheckAll()
{
	var ok = true;
	ok &= CheckNom();
	ok &= CheckPrenom();
	ok &= CheckMail1();
	ok &=CheckMail2();
	ok &=CheckPassword1();
	ok &=CheckPassword2();
	ok &=CheckVoie();
	ok &=CheckCodePostal();
	ok &=CheckVille();
	
	return ok;
}

function CheckNom()
{
	var nom = document.getElementById('nom');
	var icone = document.getElementById('nom-icone');
	var txt = document.getElementById('nom-txt');
	
	if(nom.value.length > 0)
	{
		icone.className = 'ok';
		txt.innerHTML = '';
		return true;
	}
	else
	{
		icone.className = 'bad';
		txt.innerHTML = 'Veuillez remplir ce champ.';
		return false;
	}
}

function CheckPrenom()
{
	var prenom = document.getElementById('prenom');
	var icone = document.getElementById('prenom-icone');
	var txt = document.getElementById('prenom-txt');
	
	if(prenom.value.length > 0)
	{
		icone.className = 'ok';
		txt.innerHTML = '';
		return true;
	}
	else
	{
		icone.className = 'bad';
		txt.innerHTML = 'Veuillez remplir ce champ.';
		return false;
	}
}

function CheckMail1()
{	
	var mail = document.getElementById('mail1');
	var icone = document.getElementById('mail1-icone');
	var txt = document.getElementById('mail1-txt');
	
	if(isEmail(mail.value))
	{
		icone.className = 'ok';
		txt.innerHTML = '';
		return true;
	}
	else
	{
		icone.className = 'bad';
		txt.innerHTML = 'L\'adresse mail n\'est pas valide.';
		return false;
	}
}

function CheckExistingEmail(mailuser)
{
	var mail = document.getElementById('mail1');

	if(mail.value != mailuser)
		GetPageContent('GET', 'functions/GetIsMailAdressExistingBoolean.php?mail='+mail.value, '', RegistrationCallback, null, true);
	else
	{
		CheckMail1();
		CheckMail2();
	}
}

function isEmail(myVar)
{
	var regEmail = new RegExp('^[0-9a-z._-]+@{1}[0-9a-z.-]{2,}[.]{1}[a-z]{2,5}$','i');

	return regEmail.test(myVar);
}

function CheckMail2()
{	
	var mail1 = document.getElementById('mail1');
	var mail2 = document.getElementById('mail2');
	var icone = document.getElementById('mail2-icone');
	var txt = document.getElementById('mail2-txt');
	
	if(mail1.value == mail2.value)
	{
		icone.className = 'ok';
		txt.innerHTML = '';
		return true;
	}
	else
	{
		icone.className = 'bad';
		txt.innerHTML = 'Les deux adresses mail ne sont pas identiques.';
		return false;
	}
}

function CheckPassword1()
{
	var password = document.getElementById('password1');
	var icone = document.getElementById('password1-icone');
	var txt = document.getElementById('password1-txt');
	
	if(password.value.length > 5)
	{
		icone.className = 'ok';
		txt.innerHTML = '';
		return true;
	}
	else
	{
		icone.className = 'bad';
		txt.innerHTML = 'Le mot de passe doit faire au moins 6 caractères.';
		return false;
	}
}

function CheckPassword2()
{	
	var password1 = document.getElementById('password1');
	var password2 = document.getElementById('password2');
	var icone = document.getElementById('password2-icone');
	var txt = document.getElementById('password2-txt');
	
	if(password1.value == password2.value)
	{
		icone.className = 'ok';
		txt.innerHTML = '';
		return true;
	}
	else
	{
		icone.className = 'bad';
		txt.innerHTML = 'Les deux mots de passe ne sont pas identiques.';
		return false;
	}
}

function CheckVoie()
{
	var voie1 = document.getElementById('voie1');
	var voie2 = document.getElementById('voie2');
	var icone = document.getElementById('voie-icone');
	var txt = document.getElementById('voie-txt');
	
	if(voie1.value.length > 0 || voie2.value.length > 0)
	{
		icone.className = 'ok';
		txt.innerHTML = '';
		return true;
	}
	else
	{
		icone.className = 'bad';
		txt.innerHTML = 'Veuillez remplir ce champ.';
		return false;
	}
}

function CheckCodePostal()
{
	var codepostal = document.getElementById('codepostal');
	var icone = document.getElementById('codepostal-icone');
	var txt = document.getElementById('codepostal-txt');
	
	var codepostalValue = parseInt(codepostal.value, 10);
	
	if(!isNaN(codepostalValue) && codepostalValue > 999 && codepostalValue < 100000)
	{
		icone.className = 'ok';
		txt.innerHTML = '';
		return true;
	}
	else
	{
		icone.className = 'bad';
		txt.innerHTML = 'Code postal invalide.';
		return false;
	}
}

function CheckVille()
{
	var ville = document.getElementById('ville');
	var icone = document.getElementById('ville-icone');
	var txt = document.getElementById('ville-txt');
	
	if(ville.value.length > 0)
	{
		icone.className = 'ok';
		txt.innerHTML = '';
		return true;
	}
	else
	{
		icone.className = 'bad';
		txt.innerHTML = 'Veuillez remplir ce champ.';
		return false;
	}
}

function SubmitRegistration()
{
	var mail = document.getElementById('mail1');

	GetPageContent('GET', 'functions/GetIsMailAdressExistingBoolean.php?mail='+mail.value, '', RegistrationCallback, 'create', true);	
}

function RegistrationCallback(success, text, param)
{
	var icone = document.getElementById('mail1-icone');
	var txt = document.getElementById('mail1-txt');
	
	if(success)
	{
		if(text == '0')
		{
			if(param == 'create')
				CreerCompte();
		}
		else
		{
			txt.innerHTML = 'Cette adresse existe déjà';
			icone.className = 'bad';
		}
	}
	return;
} 